// Written by Steve Andersen, copyright (c) 2007 ONE/Northwest
// This program is released under the GNU General Public License. http://www.gnu.org/licenses/
public class ONEN_TEST_Opp_Clone {
	static testMethod void testCloneOpp(){
	//create a contact
	Contact firstContact = new Contact (
		FirstName='Joe',
		LastName='Johanssen',
		MailingStreet='123 Elm St',
		MailingCity='Wauwatosa',
		MailingState='WI',
		MailingPostalCode='53213',
		MailingCountry='USA'			
	);

	system.debug ('TEST>>>>> inserting Contact 1');
	insert firstContact;
	
	//create a gift
	Opportunity opp1 = new Opportunity (
		Name='Joe Johanssen 2005 Gift',
		CloseDate=Date.newInstance(2005,10,10),
		StageName='Closed Won',
		Amount=100,
		CR_Contact_Id__c=firstContact.id,
		CR_Contact_Role__c='Individual Donor'
	);
	
	system.debug ('TEST>>>>> inserting Opp 1');
	insert opp1;
	OpportunityContactRole[] primaryContactRole2 = [select ContactId, Contact.FirstName, Contact.LastName, Role, IsPrimary from OpportunityContactRole where OpportunityId = :opp1.id and IsPrimary=true];
	
	Id NewOppId;

	system.debug ('TEST>>>>> cloning Opp 1 - 1 year out');
	ONEN_OppContactRoles.haveCheckedContactRoles = false;
	NewOppId = ONEN_OpportunityWebServices.cloneOpp(opp1.id,1,'Year');

	//system.debug('new opp id: ' + NewOppId);
	Opportunity[] ClonedOpp = [select Name, StageName, CloseDate from Opportunity where Id =:NewOppId];
	//new opp should be created with correct name
	string expectedName = 'Joe Johanssen 2006 ' + ONEN_Constants.OPP_DEFAULT_RECTYPE_FORTESTS;
	System.assertEquals(expectedName,ClonedOpp[0].Name);
	System.assertEquals(ONEN_Constants.OPP_DEFAULT_STAGE,ClonedOpp[0].StageName); 
	System.assertEquals(opp1.CloseDate.addYears(1),ClonedOpp[0].CloseDate); 
	
	//MP: test contact role. 1. correct contact 2. correct role of correct contact
	OpportunityContactRole[] primaryContactRole = [select ContactId, Contact.FirstName, Contact.LastName, Role, IsPrimary from OpportunityContactRole where OpportunityId = :NewOppId and IsPrimary=true];
	//system.debug('primary contact role: '+primaryContactRole[0]);
	
	System.assertEquals(firstContact.id, primaryContactRole[0].ContactId);
	System.assertEquals('Individual Donor', primaryContactRole[0].Role);	
	
	//clone the opp again one month out
	system.debug ('TEST>>>>> cloning Opp 1 - 1 month out');
	ONEN_OppContactRoles.haveCheckedContactRoles = false;
	NewOppId = ONEN_OpportunityWebServices.cloneOpp(opp1.id,1,'Month');
	Opportunity[] ClonedOpp2 = [select Name, StageName, CloseDate from Opportunity where Id =:NewOppId];
	//new opp should be created with correct name
	System.assertEquals(opp1.CloseDate.addMonths(1),ClonedOpp2[0].CloseDate);
	//system.debug(ClonedOpp2[0].Name);
	
	}

}